#!/usr/bin/python

import os, subprocess
import glob
from os import system
from shutil import copyfile
import re
import string
import sys

system('clear') # Clear the screen and start fresh
rs=system('make')  # need the exit status here...
if rs!=0: sys.exit("\n\n *** Compilation of enumlib failed *** \n")
rs=system('make makestr.x')  # need the exit status here...
if rs!=0: sys.exit("\n\n *** Compilation of makestr.x failed *** \n")

#tests=glob.glob('tests/struct_enum.in.*')
#f=open('tests/list')

system('rm -r vasp.000007')
print "\n *** testing on of Rod's gang of 13 ***"
system('./makestr.x ./tests/13struct.out 7')
rs=system('diff -q vasp.000007 tests/vasp.0007_from2x3_Rods_case')
if rs!=0:
    sys.exit("\n --- Failure in the first of Rod's 2x3 cases ---\n")
else:
    print "\n <<< First test of makestr.x passed >>>\n"

# Loop over the 17 fcc superstructures that have 2-4 atoms/cell. For pictures, see
# "Where are nature's missing structures" Nat. Mat. 2008 or "Algorithm for generating
# derivative superstructures" PRB 2008.
poscar = sorted(glob.glob('tests/*.poscar'))
f = open('tests/17list')
rs=system('make find_structure_in_list.x')  # need the exit status here...
if rs!=0:
    sys.exit("\n --- Didn't compile ---\n")
print "\n *** Testing compare code on first 17 fcc structures ***"
if rs!=0: sys.exit("\n\n *** Compilation of find_structure_in_list.x failed *** \n")
for ip in poscar:
    print ip
    command = './find_structure_in_list.x '+ip+' tests/struct17fcc.out'
    #process = subprocess.Popen('pwd')
    process = subprocess.Popen(command,stdout=subprocess.PIPE, shell=True)
    os.waitpid(process.pid, 0)
    #print process.stdout.read()
    #print process.stdout.read().strip() \w+([0-9]+)
    r1 = re.compile('Structure #: \s+([0-9]+)')
    m = r1.match(process.stdout.read().strip())
    nStr =  m.group(1)
    #print f.readline().strip()+"X"+nStr+"X
    if(f.readline().strip()!=nStr):
        sys.exit("\n\nFailure in test")
    #print "test passed"

print "\n <<< Test of 17 fcc with compare code passed >>>\n"

# Test to see if the poscar with Direct coordinates works correctly
# This test came from a bug discovered on 4/16/2010
print "\n *** Testing one poscar with direct coordinates (rather than Cartesian) ***"
command = './find_structure_in_list.x tests/poscar.direct tests/struct17fcc.out'
process = subprocess.Popen(command,stdout=subprocess.PIPE, shell=True)
os.waitpid(process.pid, 0)
r1 = re.compile('Structure #:\s+(15)') # Need the parentheses to "mark" the group (split the match
# into pieces)
m = r1.match(process.stdout.read().strip())
nStr =  m.group(1)
if("15"!=nStr):
    sys.exit("\n\nFailure in test")
print "\n <<< Test with direct coordinates passed >>>\n"

# Tests to see if a multilattice-derived superstructure (hcp in this case can be found properly)
# This test came from discovery that the find_labeling_from_atomic_basis was not fully general
# and that the compare code didn't work if the POSCAR and struct_enum file didn't use the same
# origin 5/7/2010
print "\n *** Testing the D0_19 structure. POSCAR generated by aconvasp. Origin is shifted relative"
print "to the parent lattice in the struct_enum.out file ***"
command = './find_structure_in_list.x tests/D0_19 tests/struct_enum.out.hcp_500'
process = subprocess.Popen(command,stdout=subprocess.PIPE, shell=True)
os.waitpid(process.pid, 0)
r1 = re.compile('Structure #:\s+(332)') # Need the parentheses to "mark" the group (split the match
# into pieces)
m = r1.match(process.stdout.read().strip())
nStr =  m.group(1)
if("332"!=nStr):
    sys.exit("\n\nFailure in test")
print "\n <<< Test with shifted origin and multilattice passed >>>\n"

# Same as previous test but the definition of the parent lattice in the struct_enum.out is different
# (but identical). Added on 5/12/2010.
print "\n *** Testing the D0_19 structure. POSCAR generated by aconvasp. Origin is shifted relative"
print "to the parent lattice in the struct_enum.out file ***"
command = './find_structure_in_list.x tests/D0_19 tests/struct_enum.out.hcp.binary.4_4'
process = subprocess.Popen(command,stdout=subprocess.PIPE, shell=True)
os.waitpid(process.pid, 0)
r1 = re.compile('Structure #:\s+(269)') # Need the parentheses to "mark" the group (split the match
# into pieces)
m = r1.match(process.stdout.read().strip())
nStr =  m.group(1)
if("269"!=nStr):
    sys.exit("\n\nFailure in test")
print "\n <<< Second test with shifted origin and multilattice passed >>>\n"
